import com.zto.sch.beanfactoryprocessor.MyBeanFactoryProcessor;
import com.zto.sch.dao.CardDao;
import com.zto.sch.dao.UserDao;
import com.zto.sch.dao.UserDaoImpl;
import com.zto.sch.dao.UserDaoImpl1;
import com.zto.sch.interceptor.MyMethodInterceptor;
import com.zto.sch.service.CardService;
import org.junit.Test;
import org.springframework.cglib.core.SpringNamingPolicy;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @program: javatest
 * @description:
 * @author: sch
 * @create: 2020-07-03 16:46
 **/
public class TestClass {
    @Test
    public void test() {
        CardDao cardDao = (CardDao) Proxy.newProxyInstance(TestClass.class.getClassLoader(), new Class[]{CardDao.class}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println("aaa");
                return null;
            }
        });

        cardDao.query();
        System.out.println("over");
    }

    @Test
    public void test1() {
        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext(AppConfig.class);
        UserDao bean = (UserDao) context.getBean("userDao1");
        bean.save();
    }

    @Test
    public void test2() {
        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext(UserDaoImpl1.class);
        UserDao bean = (UserDao) context.getBean("userDao1");
        bean.save();
    }

    @Test
    public void test3() {
        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext();
        context.register(UserDaoImpl.class);
        context.refresh();
        UserDao bean = (UserDao) context.getBean("userDao");
        bean.save();
    }

    @Test
    public void test4() {
        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext();
        context.addBeanFactoryPostProcessor(new MyBeanFactoryProcessor());
        //构建bean工厂
        context.register(AppConfig.class);
//        context.scan("com.abc");
        //初始化spring环境
        context.refresh();

        //完成了扫描，但完成扫描工作的不是AnnotationConfigApplicationContext的scanner
        UserDao bean = (UserDao) context.getBean("userDao");
        UserDao bean2 = (UserDao) context.getBean("userDao");
        bean.save();
        System.out.println(bean.hashCode() + "::" + bean2.hashCode());
    }


    @Test
    public void test5() {
        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext(AppConfig.class);
        CardService cardService = (CardService) context.getBean("cardService");
        cardService.query();
//        CardDao cardDao = (CardDao) context.getBean("cardDao");
//        cardDao.query();
//        UserDaoImpl2 userDaoImpl2 = context.getBean(UserDaoImpl2.class);
//        userDaoImpl2.save();

    }

    @Test
    public void test6() {
        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext(AppConfig.class);
        UserDao userDao1 = (UserDao) context.getBean("userDao1");
        userDao1.save();
        UserDao userDao = (UserDao) context.getBean("userDao");
        userDao.save();
    }

    @Test
    public void test7() {
        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext(AppConfig.class);
    }

    @Test
    public void testSpringCglig() {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(UserDaoImpl1.class);
        enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
        enhancer.setCallback(new MyMethodInterceptor());
        UserDaoImpl1 userDaoImpl1 = (UserDaoImpl1) enhancer.create();
        userDaoImpl1.save();
    }

    @Test
    public void test9() {
        List<String> list = Stream.of("abc","aaa","ccc").collect(Collectors.toList());
//        List<String> b = list.stream().filter(s -> s.equals("b")).collect(Collectors.toList());
        list.stream().forEach(e->{
            if("abc".equals(e)){
                return;
            }
            System.out.println("哈哈");
        });
//        System.out.println(b);
    }
}
